package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.service.AlertDashboardService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

/**
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/alert")
public class AlertDashboardController2 {

    @Autowired
    private AlertDashboardService alertDashboardService;

    @Data
    public static class Param {
        private String interval;
    }

    @RestGrpcMapping("/listTopicDashboard.do")
    public NTSTATUS listTopicDashboard(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            HashMap<String, String> paramMap = new HashMap<>();
            if (aa.getInterval() != null) {
                paramMap.put("interval", aa.getInterval());
            } else {
                paramMap.put("interval", "day");
            }
            result.setData(alertDashboardService.listTopicDashboard(paramMap));
            log.info("success.");
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/listRuleDashboard.do")
    public NTSTATUS listRuleDashboard(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            HashMap<String, String> paramMap = new HashMap<>();
            if (aa.getInterval() != null) {
                paramMap.put("interval", aa.getInterval());
            } else {
                paramMap.put("interval", "day");
            }
            result.setData(alertDashboardService.listRuleDashboard(paramMap));
            log.info("success.");
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

}
